home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / mousport.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  10KB  |  254 lines

  1. /****************************************************************
  2. *                                                               *
  3. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  4. * No part of this program may be reproduced, transmitted,       *
  5. * transcribed, stored in retrieval system, or translated into   *
  6. * any language or computer language, in any form or by any      *
  7. * means, electronic, mechanical, magnetic, optical, chemical,   *
  8. * manual or otherwise, without the prior written permission of  *
  9. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  10. * Los Gatos, CA 95030                                           *
  11. *                                                               *
  12. ****************************************************************/
  13.  
  14. /* *********************************************************************** */
  15. /* mouse test, for right game port on the Amiga.
  16.  
  17.    Notes:  The right port is used for this test because the input.device
  18.    task is busy continuously with the lefthand port, feeding input events
  19.    to intuition or console devices.  If Intuition is not activated
  20.    (applications which take over the whole machine may decide not to
  21.    activate Intuition), and if no console.device is activated either,
  22.    the input.device will never activate... allowing the application
  23.    free reign to use either the left OR the right hand joystick/mouse
  24.    port.  If either intuition or the console device are activated,
  25.    the lefthand port will yield, at best, every alternate input
  26.    event to an external application such as this test program.
  27.    This will undoubtedly mess up either of the two applications
  28.    and should therefore be avoided.  It was ok to use the right
  29.    port in this case, since the system has no particular interest
  30.    in monitoring it.
  31.  
  32. *********************************************************************** */
  33.  
  34.  
  35. /* #include "intuall.h" */
  36.  
  37. #include <exec/types.h>
  38. #include <graphics/gfx.h>
  39. #include <devices/gameport.h>
  40. #include <devices/inputevent.h>
  41.  
  42. LONG GfxBase=0;
  43.  
  44. #define XMOVE 10
  45. #define YMOVE 10
  46. #define MAX(m,n) (m > n ? m : n)
  47.  
  48. struct GamePortTrigger mousetrigger = {
  49.                                         GPTF_UPKEYS + GPTF_DOWNKEYS,
  50.                                         1800,
  51.                                         XMOVE,
  52.                                         YMOVE };
  53.         /* trigger on all mouse key transitions, and every 30 seconds, and
  54.                                    for any 10 in an x or y direction */
  55.  
  56. struct InputEvent *game_data;   /* pointer into the returned data area
  57.                                          * where input event has been sent */
  58. SHORT           error;
  59.  
  60. struct IOStdReq *game_io_msg;
  61.  
  62. BYTE            gamebuffer[sizeof( struct InputEvent )];
  63. BYTE            *gamedata;
  64.  
  65. SHORT           testval;
  66.         
  67. struct Port     *game_msg_port;
  68.  
  69. SHORT movesize;
  70. extern struct Port *CreatePort();
  71. extern struct IOStdReq *CreateStdIO();          
  72.  
  73. SHORT codeval, timeouts;
  74.  
  75. #define IF_NOT_IDLE_TWO_MINUTES while(timeouts < 4)
  76.  
  77. main()
  78. {
  79.  
  80.         GfxBase = OpenLibrary("graphics.library", 0);
  81.         if (GfxBase == NULL)
  82.         {
  83.                 printf("Unable to open graphics library\n");
  84.                 exit(1000);
  85.         }
  86.         printf("Mouseport Demo\n");
  87.         printf("\nMove Mouse from Left Port to Right Port\n");
  88.         printf("\nThen move the mouse and click its buttons");
  89.  
  90.         
  91.         timeouts = 0;
  92.  
  93.         gamedata = &gamebuffer[0];      
  94.                 /* point to first location in game buffer */
  95.  
  96.         game_msg_port = CreatePort(0,0);        
  97.                 /* provide a port for the IO response */
  98.         if(game_msg_port == 0)
  99.                 {
  100.                 printf("\nError While Performing CreatePort");
  101.                 exit(-1);
  102.                 }
  103.  
  104.         game_io_msg = CreateStdIO(game_msg_port);     
  105.                 /* make an io request block for communicating with
  106.                                 the keyboard */
  107.  
  108.         if(game_io_msg == 0)
  109.                 {
  110.                 printf("\nError While Performing CreateStdIO");
  111.                 DeletePort(game_msg_port);
  112.                 exit(-2);
  113.                 }
  114.  
  115.         error = OpenDevice("gameport.device",1,game_io_msg,0);
  116.                 /* open the device for access, unit 1 is right port */
  117.  
  118.         if(error != 0)
  119.                 {
  120.                 printf("\nError while opening the device, exiting");
  121.                 DeleteStdIO(game_io_msg);
  122.                 DeletePort(game_msg_port);
  123.                 exit(-3);
  124.                 }
  125.  
  126.         game_io_msg->io_Length = sizeof(struct InputEvent);     
  127.                 /* read one event each time we go back to the gameport */
  128.  
  129.         game_io_msg->io_Data = gamebuffer;      
  130.                 /* show where to put the data when read */
  131.  
  132.         game_data = (struct InputEvent *)gamebuffer;
  133.  
  134.                 /* test the mouse in this loop */
  135.         
  136.         set_controller_type(GPCT_MOUSE);
  137.  
  138.         game_io_msg->io_Command = GPD_SETTRIGGER;       
  139.                 /* specify the trigger conditions */
  140.         game_io_msg->io_Data = &mousetrigger;   
  141.                 /* show where to find the trigger condition info */
  142.                 /* this command doesn't wait... returns immediately */
  143.         SendIO(game_io_msg);    
  144.         WaitPort(game_msg_port);
  145.         GetMsg(game_msg_port);
  146.         printf("\nI will report:");
  147.         printf("\n     Mouse X or Y moves if either is over 10 counts");
  148.         printf("\n     Button presses (along with mouse moves if any)");
  149.         printf("\n     Or every 30 seconds (along with mouse moves if any)");
  150.         printf("\n          if neither move or click happens\n");
  151.  
  152.         printf("\nIf no activity for 2 minutes, the program exits\n");
  153.         game_io_msg->io_Command = GPD_READEVENT;        
  154.                 /* from now on, just read input events */
  155.         game_io_msg->io_Data = gamebuffer;              
  156.                 /* into the input buffer, one at a time. */
  157.                 /* read-event waits for the preset conditions */
  158.  
  159.     IF_NOT_IDLE_TWO_MINUTES
  160.         {
  161.         game_io_msg->io_Length = sizeof(struct InputEvent);     
  162.                 /* read one event each time we go back to the gameport */
  163.  
  164.         printf("\n Waiting For Mouse Report\n");
  165.         SendIO(game_io_msg);
  166.         WaitPort(game_msg_port);
  167.                 /* this is NOT a busy wait... it is a task-sleep */
  168.         GetMsg(game_msg_port);
  169.  
  170.         codeval = game_data->ie_Code;
  171.         switch(codeval) 
  172.                 {
  173.                 case IECODE_LBUTTON:
  174.                         printf("\nMouse Left Button Pressed");
  175.                         maybe_mouse_moved();
  176.                         break;
  177.                 case IECODE_RBUTTON:
  178.                         printf("\nMouse Right Button Pressed");
  179.                         maybe_mouse_moved();
  180.                         break;
  181.                 case (IECODE_LBUTTON + IECODE_UP_PREFIX):
  182.                         printf("\nMouse Left Button Released");
  183.                         maybe_mouse_moved();
  184.                         break;
  185.                 case (IECODE_RBUTTON + IECODE_UP_PREFIX):
  186.                         printf("\nMouse Right Button Released");
  187.                         maybe_mouse_moved();
  188.                         break;
  189.                 case IECODE_NOBUTTON:
  190.                         timeouts++;     /* after 2 minutes, dump program 
  191.                                          * if user loses interest 
  192.                                          */
  193.                         movesize = maybe_mouse_moved();
  194.                         if(movesize == 0)
  195.                            printf("\n30 seconds passed, no trigger events");
  196.                         else if(movesize < XMOVE && movesize < YMOVE )
  197.                            {
  198.                            printf("\n(Even though less than trigger count,");
  199.                            printf("\n reporting mouse move at the selected");
  200.                            printf("\n timing interval for user info)");
  201.                            }
  202.                         break;
  203.                 default:
  204.                         break;
  205.                 }
  206.         }
  207.  
  208.         set_controller_type(GPCT_NOCONTROLLER);
  209.  
  210.         CloseDevice(game_io_msg);
  211.         DeleteStdIO(game_io_msg);
  212.         DeletePort(game_msg_port);
  213.         
  214.         printf("\nExiting program... 2 minutes with no activity sensed\n1> ");
  215.         return;
  216. }               
  217.  
  218.         /* if mouse didnt move far enough to trigger a report, then caller 
  219.          * will also report that 30 seconds (1800 vblanks) has elapsed 
  220.          */
  221.  
  222. int maybe_mouse_moved()         
  223. {
  224.         SHORT xmove, ymove;
  225.         xmove = game_data->ie_X;
  226.         ymove = game_data->ie_Y;
  227.  
  228.         if(xmove != 0 || ymove != 0)
  229.                 {
  230.                 printf("\nMouse Moved by X-value %ld, Y-value %ld",
  231.                         xmove, ymove);
  232.                 timeouts = 0;
  233.                 }
  234.         if(xmove < 0) xmove = -xmove;
  235.         if(ymove < 0) ymove = -ymove;
  236.  
  237.         return(MAX(xmove,ymove));
  238. }
  239.  
  240. int set_controller_type(type)
  241. SHORT type;
  242. {
  243.         game_io_msg->io_Command = GPD_SETCTYPE;   
  244.                 /* set type of controller to mouse */
  245.         *gamedata = type;       
  246.                 /* this means dont generate any more reports */
  247.         SendIO(game_io_msg);    
  248.                 /* set it up */
  249.                 /* this command doesn't wait... returns immediately */
  250.         WaitPort(game_msg_port);
  251.         GetMsg(game_msg_port);
  252.         return(0);
  253. }
  254.